package com.zhf.JDBC01;

import java.sql.*;
import java.util.Date;

/**
 * @Author: ZhaoHuanfeng
 * @Date: 2020/9/22 23:51
 */
public class JDBCDemo03 {

    public static void main(String[] args) {

        ResultSet resultSet=null;
        Statement statement=null;
        Connection con=null;
        try {
            //注册驱动(可以省略)
            Class.forName("com.mysql.jdbc.Driver");

            //获取连接
            String url="jdbc:mysql://localhost:3306/db4?characterEncoding=UTF-8";
            con= DriverManager.getConnection(url,"root","12345");

            //获取语句执行对象
            statement = con.createStatement();

            //执行sql
            String sql="selelct * from jdbc_user";
            resultSet = statement.executeQuery(sql);

            //处理结果集
            while (resultSet.next()){
                //获取id
                int id2=resultSet.getInt("id");
                //获取姓名
                String name=resultSet.getString("username");
                //获取密码
                String password=resultSet.getString("password");
                //获取生日
                Date birthday=resultSet.getDate("birthday");
                System.out.println(id2+" - "+name+" - "+password+" - "+birthday);
            }
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }finally {
            if (resultSet!=null){
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (statement!=null){
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (con!=null){
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }




    }
}
